package pwd.allen.fastdfs.utils;

import cn.hutool.core.io.IoUtil;
import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * TrackerClient - TrackerServer接口
 * GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
 * FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
 * AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)
 *
 * @author licyaae
 * @date 2021/06/02
 */
@Component
public class FileDfsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
    @Resource
    private FastFileStorageClient storageClient;

    /**
     * 上传文件
     */
    public String upload(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return storePath.getFullPath();
    }

    /**
     * 上传图片并生成缩略图
     * 看方法注释说支持的图片格式包括"JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
     */
    public String uploadImg(MultipartFile file) throws IOException {
        FastImageFile fastImageFile = new FastImageFile.Builder()
                .withFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()))
//                .withThumbImage(100, 60)        // 缩略图用尺寸的方式
                .withThumbImage(0.5)        // 缩略图用缩小比例的方式
                .build();
        StorePath storePath = storageClient.uploadImage(fastImageFile);
        return storePath.getFullPath();
    }

    /**
     * 查询文件信息
     *
     * 格式：source_ip_addr = 192.168.118.102, file_size = 419290, create_timestamp = 1970-01-20 03:50:12, crc32 = -715551042
     */
    public FileInfo queryFileInfo(String fileUrl) {
        if (!StringUtils.hasLength(fileUrl)) {
            LOGGER.info("fileUrl == >>文件路径为空...");
            return null;
        }
        FileInfo fileInfo = null;
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            fileInfo = storageClient.queryFileInfo(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
        }
        return fileInfo;
    }

    /**
     * 删除文件
     */
    public void deleteFile(String fileUrl) {
        if (!StringUtils.hasLength(fileUrl)) {
            LOGGER.info("fileUrl == >>文件路径为空...");
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
        }
    }

    /**
     * 下载文件
     * @param fileUrl
     * @return
     */
    public byte[] downloadFile(String fileUrl) {
        if (!StringUtils.hasLength(fileUrl)) {
            LOGGER.info("fileUrl == >>文件路径为空...");
            return null;
        }
        byte[] bytes = null;
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            bytes = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), ins -> IoUtil.readBytes(ins));
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
        }
        return bytes;
    }
}

